#include "forgrt.h"
#include "ui_forgrt.h"

#include"execsql.h"

#include "login.h"
#include <QMessageBox>
#include <QString>
#include<QSqlQuery>
#include <QSqlRecord>
#include<QDebug>
//#include"student.h"

Forgrt::Forgrt(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Forgrt)
{
    ui->setupUi(this);
}

Forgrt::~Forgrt()
{
    delete ui;
}
void Forgrt::showForgrt()
  {
    emit signalForgrt();
    this->show();
  }

void Forgrt::showLW()
{
    Login *lw = new Login;
    lw->show();
}

void Forgrt::on_pb_ok_clicked()
{
    id =ui->lineEdit_id->text();
     ExecSQL exe;
     switch (data) {
     case 1:
         ainfo = exe.selectAdminInfo(id);
          if(id == ainfo.getID() )
          {
              if(ui->lineEdit_name->text() == ainfo.getname())
              {
                  if(ui->lineEdit_pswd->text()== ui->lineEdit_again->text())
                  {
                      QSqlQuery query;
                      QString queryString =
                              QString("update AdmintorInfoTable set loginpw="+ui->lineEdit_again->text() +
                                      "where ID = '%1'").arg(ui->lineEdit_id->text());
                      if(query.exec(queryString))
                      {
                          int id_id = query.record().indexOf("ID");
                          int Name_id = query.record().indexOf("Name");
                          int Sex_id = query.record().indexOf("Sex");
                          int Birth_id = query.record().indexOf("Birth");
                          int pswd_id = query.record().indexOf("loginpw");
                          while(query.next())
                          {
                              QString id = query.value(id_id).toString();
                              QString Name = query.value(Name_id).toString();
                             QString pswd = query.value(pswd_id).toString();
                              QString Sex = query.value(Sex_id).toString();
                              QString Birth = query.value(Birth_id).toString();
                               ainfo.updateAdminInfo(id,Name,Sex, Birth, pswd);
                            }
                  }
                  }
              }
          }
         break;
     case 3:
         sinfo = exe.selectStudentInfo(id);
          if(id == sinfo.getID() )
          {
              if(ui->lineEdit_name->text() == sinfo.getname())
              {
                  if(ui->lineEdit_pswd->text()== ui->lineEdit_again->text())
                  {
                      QSqlQuery query;
                      QString queryString =
                              QString("update StudentInfoTable set loginpw = "+ui->lineEdit_again->text() +
                                      " where ID  = '%1'").arg(id);
                      if(query.exec(queryString))
                      {
                          int ID_id = query.record().indexOf("ID");
                          int xuehao_id = query.record().indexOf("xuehao");
                          int Name_id = query.record().indexOf("Name");
                          int Sex_id = query.record().indexOf("Sex");
                          int Classid_id = query.record().indexOf("Classid");
                          int loginpw_id = query.record().indexOf("loginpw");
                          int RxTime_id = query.record().indexOf("RxTime");
                          while(query.next())
                          {
                              QString id = query.value(ID_id).toString();
                              QString Name = query.value(Name_id).toString();
                              QString XuehaoID = query.value(xuehao_id).toString();
                              QString pswd = query.value(loginpw_id).toString();
                              QString Classid = query.value(Classid_id).toString();
                              QString Sex = query.value(Sex_id).toString();
                              QString rxtime = query.value(RxTime_id).toString();
                              sinfo.updateStudentInfo(id, Name,XuehaoID, pswd,Sex, Classid, pswd);
                              sinfo.display();
                          }
                }
                }
            }
        }
  break;
     case 2:
         tinfo = exe.selectTeacherInfo(id);
          if(id == tinfo.getID() )
          {
              if(ui->lineEdit_name->text() == tinfo.getName())
              {
                  if(ui->lineEdit_pswd->text()== ui->lineEdit_again->text())
                  {
                      QSqlQuery query;
                      QString queryString =
                              QString("update TeacherInfoTable set loginpw = "+ui->lineEdit_again->text() +
                                      " where ID  = '%1'").arg(id);
                      if(query.exec(queryString))
                      {
                          int id_id = query.record().indexOf("ID");
                          int TeacherId_id = query.record().indexOf("TeacherID");
                          int XID_id = query.record().indexOf("XID");
                          int Name_id = query.record().indexOf("Name");
                          int Sex_id = query.record().indexOf("Sex");
                          int Birth_id = query.record().indexOf("Birth");
                          int pswd_id = query.record().indexOf("loginpw");
                          while(query.next())
                          {
                              QString id = query.value(id_id).toString();
                              QString Name = query.value(Name_id).toString();
                              QString XID = query.value(XID_id).toString();
                              QString pswd = query.value(pswd_id).toString();
                              QString TeacherID = query.value(TeacherId_id).toString();
                              QString Sex = query.value(Sex_id).toString();
                              QString Birth = query.value(Birth_id).toString();
                               tinfo.updateTeacherInfo(id, TeacherID,XID, Name,Sex, Birth, pswd);
                               tinfo.display();
                            }
                  }
                  }
              }
          }
  break;
     default:
         QMessageBox msgBox;
         msgBox.setText("Error: Your ID is nonexistent !!");
         msgBox.setInformativeText("Please try again !");
         msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
         msgBox.setDefaultButton(QMessageBox::Retry);

         int ret = msgBox.exec();
         switch (ret) {
           case QMessageBox::Retry:
               ui->lineEdit_id->setFocus();
               break;
           case QMessageBox::Cancel:
               this->close();
               break;
           default:
               // should never be reached
               break;
         }

         break;


}

}

void Forgrt::on_pb_cancel_clicked()
{
       this->close();
       this->showLW();
  }
